package com.meichis.mcsappframework.http;

import android.net.ParseException;

import com.google.gson.JsonParseException;
import com.meichis.mcsappframework.BuildConfig;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;

import retrofit2.HttpException;

/**
 * Created by jsrgc on 2017/6/1.
 */

public class CallException {
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;
    private static final int ACCESS_DENIED = 302;
    private static final int HANDEL_ERRROR = 417;

    public static Throwable handleException(Throwable e) {
        if (BuildConfig.DEBUG) {
            e.printStackTrace();
        }
        Throwable ex;
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            switch (httpException.code()) {
                case UNAUTHORIZED:
                    ex = new Throwable("未授权的请求");
                    break;
                case FORBIDDEN:
                    ex = new Throwable("禁止访问");
                    break;
                case NOT_FOUND:
                    ex = new Throwable("服务器地址未找到");
                    break;
                case REQUEST_TIMEOUT:
                    ex = new Throwable("请求超时");
                    break;
                case GATEWAY_TIMEOUT:
                    ex = new Throwable("网关响应超时");
                    break;
                case INTERNAL_SERVER_ERROR:
                    ex = new Throwable("服务器出错");
                    break;
                case BAD_GATEWAY:
                    ex = new Throwable("无效的请求");
                    break;
                case SERVICE_UNAVAILABLE:
                    ex = new Throwable("服务器不可用");
                    break;
                case ACCESS_DENIED:
                    ex = new Throwable("网络错误");
                    break;
                case HANDEL_ERRROR:
                    ex = new Throwable("接口处理失败");
                    break;
                default:
                    ex = new Throwable(e.getMessage());
                    break;
            }
            return ex;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            ex = new Throwable("解析错误");
            return ex;
        } else if (e instanceof ConnectException) {
            ex = new Throwable("连接失败");
            return ex;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            ex = new Throwable("证书验证失败");
            return ex;
        } else if (e instanceof java.security.cert.CertPathValidatorException) {
            ex = new Throwable("证书路径没找到");
            return ex;
        } else if (e instanceof ConnectTimeoutException || e instanceof java.net.SocketTimeoutException || e instanceof java.io.IOException) {
            ex = new Throwable("连接超时");
            return ex;
        } else if (e instanceof java.lang.ClassCastException) {
            ex = new Throwable("类型转换出错");
            return ex;
        } else if (e instanceof NullPointerException) {
            ex = new Throwable("数据有空");
            return ex;
        } else {
            return new Throwable(e.getMessage());
        }
    }
}
